﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;
using ToDoList.Logic;

namespace ToDoListTest
{
    class UserExamine
    {
        private static TodoContext _ctx = new TodoContext();
        /// <summary>
        /// 用户主界面
        /// </summary>
        public void FirstInterface()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(" \t\t1 LoginInterFace //登录\t\t \n" + " \t\t2 RegisterInterface //注册 \t\t\n" +
                                  " \t\t3 Exit \n\t");
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                Console.Clear();
                switch (choose)
                {
                    case 1: LoginInterFace(); break;
                    case 2: RegisterInterface(); break;
                    case 3: return;
                    default: break;
                }
            }
        }
        /// <summary>
        /// //登录界面 
        /// </summary>
        public void LoginInterFace()
        {
            while (true)
            {
                Console.Clear();
                string  userName = InputRequire.InputString("Enter the user name:");
                string password = InputRequire.InputString("Enter the password :");
                Console.Clear();
                UserFunction userFunction = new UserFunction();
                try
                {
                    Users user = userFunction.Login(userName, password);
                    if (user != null)
                    {
                        //进入用户功能界面
                        UserExamine userExamine = new UserExamine();
                        userExamine.UserInventoryFunction(user);
                    }
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                int choose = InputRequire.InputInt("Do you want to continue:(1.yes,2.No):");
                        if (choose == 2)
                             break;
            }
        }
        /// <summary>
        /// //注册界面
        /// </summary>
        /// 
        public void RegisterInterface()
        {

            #region 输入注册所需要的信息

            string name = InputRequire.InputString("Enter the name:");
            string password = InputRequire.InputString("Enter the password :");
            string nickName = InputRequire.InputString("Enter the nickName :");
            #endregion
    
            #region 判断是否注册成功
            UserFunction userFunction = new UserFunction();
            try {
                userFunction.Enroll(name, password, nickName);
                Console.WriteLine("Your id is " + _ctx.Users.Max(p=>p.Id ) );
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
            #endregion
        }
        /// <summary>
        /// //用户清单界面 
        /// </summary>
        /// <param name="user"></param>
        public void UserInventoryFunction(Users user)
        {
            Console.Clear();
            while (true)
            {
                Console.WriteLine("\t\t\t1 Check all the inventory\\ 查看所有的清单 \n\t" +
                                 " \t\t2 Modity a inventory name\\修改清单名称 \n\t" +
                                 "\t\t3 Create a inventory \\创建一个清单\n\t" + 
                                 "\t\t4 Find a inventory\\寻找清单 \n\t"+ 
                                 "\t\t5 Delete a inventory \n\t"+
                                   "\t\t6 Enter  the backlog things interface\\进入事件界面\n\t" + "\t\t7 Exit \n\t"
                                 );
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                InventoryFunction userInventory = new InventoryFunction();
                switch (choose)
                {
                    case 1:
                        {
                            var inventorys = userInventory.GetAllInventory(user.Id);
                            foreach (var item in inventorys)
                            {
                                   Console.WriteLine($"清单名：{item.Name}\t清单ID：{item.Id} \t" +
                                        $"清单创建时间{item.CreationTime }\t 用户ID:{item.UserId}\t\n");
                            }
                        }
                        break;
                    case 2:
                        {
                            string name = InputRequire.InputString("Please input the inventory new name:");
                            int inventoryId = InputRequire.InputInt("Please input the invnetory id");
                            ConsloveFunction(userInventory.ModifyInventoryName(user.Id, inventoryId, name));
                        }
                        break;
                    case 3: userInventory.AddInventory(user.Id, InputRequire.InputString("Please input the new inventorys name:"));
                            break;
                    case 4:
                        {
                            int inventoryId = InputRequire.InputInt("Please input the inventory id");
                            var inventory = userInventory.FindInventory(user.Id, inventoryId);
                            Console.WriteLine($"清单名：{inventory.Name }\t清单ID：{inventory.Id  }" +
                                      $"清单创建时间{inventory.CreationTime}\t 用户ID:{inventory.UserId}");
                        }
                        break;
                    case 5:
                        userInventory.DeleteInventory(user.Id , InputRequire.InputInt("Please input delete inventory id")); break;
                    case 6: EnrollInventoryInterface(InputRequire.InputInt("Please input the inventory id"),user); break;
                    case 7: return;
                    default: break;
                }
            }

        }
        /// <summary>
        /// 用户事件界面
        /// </summary>
        /// <param name="user"></param>
        public void EnrollInventoryInterface(int inventoryId,Users user)
        {
            Console.Clear();
            bool index = false;
            while (true)
            {
                Console.WriteLine(" \t\t\t 1  Add a backlog thing \\添加事项\n\t" +
                                    "\t\t 2 Delete a backlog thing \\删除事项\n\t" +
                                    "\t\t3 Checkall the backlog things\\统计所有的事件\n\t" +
                                   " \t\t4 Statistics all the unfinsihed things\\统计未完成事项   \n\t " +
                                    "\t\t5 Complete the backlog things\\完成某一待办事项 \n\t" +
                                    "\t\t6 Cancel the complete things\\取消完成已完成事项\n\t" +
                                    "\t\t7 Add over time\\添加过期时间  \n\t" +
                                    "\t\t8Add remark \\添加备注Change overtime \n\t" +
                                    "\t\t9 Find the backolg thing \\查找事件\n\t" +
                                    " \t\t10Exit 退出 \n\t ");
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                EventFunction userEvents = new EventFunction();
                switch (choose)
                {
                    case 1:
                        {
                            string content = InputRequire.InputString("Input the content thing");
                            string remark = InputRequire.InputString("Input the remark");
                            DateTime time = InputRequire.InputDateTime("Input the Overtime(eg:yyyy-MM-dd)");
                             ConsloveFunction(userEvents.AddEvent(user.Id , inventoryId, content, time, remark));
                        }
                        break;
                    case 2:
                        userEvents.DeleteEvent(InputRequire.InputInt("Input the backlog ID:"));
                        break;
                    case 3:
                        ConsloveWritelineListBacklog(userEvents.GetAllEvents(inventoryId ,user.Id));
                        break;
                    case 4:
                        ConsloveWritelineListBacklog(userEvents.GetUnfinishedEvent(inventoryId, user.Id));
                        break;
                    case 5:
                        ConsloveFunction(userEvents.ChangeEventSate(true,InputRequire.InputInt("Input the backlog ID:")));
                        break;
                    case 6:
                        ConsloveFunction(userEvents.ChangeEventSate(false,InputRequire.InputInt("Input the backlog ID:")));
                        break;
                    case 7:
                        ConsloveFunction(userEvents.AddOverTime(InputRequire.InputInt("Input the backlog ID:"), InputRequire.InputDateTime("Input the datetime :")));
                        break;
                    case 8:
                        ConsloveFunction(userEvents.AddRemark(InputRequire.InputInt("Input the backlog ID:"), InputRequire.InputString("Input the remark:")));
                        break;
                    case 9:
                        ConsloveWritelineBacklog(userEvents.FindEvent(user.Id));
                        break;
                    case 10: index = true;break;
                    default: break;
                }
                if(index)
                    break;
            }
        }

        public void ConsloveFunction(bool result)
        {
            if (result)
                Console.WriteLine("Succeed!");
            else
                Console.WriteLine("Default");
            Console.Clear();
        }
        public void ConsloveWritelineBacklog(Matters backlog)
        {
            Console.WriteLine($"事件用户ID：{backlog.UserId }  事件清单ID：{backlog.InventoryId  }  事件ID：{backlog.Id }" +
                $"  事件创建时间{backlog.CreationTime}  事件结束时间：{backlog.OverTime} 事件：{backlog.MatterName  }" +
                $"  事件备注： {backlog.Reamrk }\n");
        }
        public void ConsloveWritelineListBacklog(List<Matters> listBacklog)
        {
            foreach (var item in listBacklog)
                ConsloveWritelineBacklog(item);
        }
    }
}
